﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Csla.Data;
using Csla;

namespace Kiosk.Library
{
    public partial class LocationReadOnlyList
    {
        /// <summary>
        /// Returns all the locations that start with the string (not case sensitive)
        /// </summary>
        public static LocationReadOnlyList GetAllStartingWith(string start)
        {
            return DataPortal.Fetch<LocationReadOnlyList>(new SingleCriteria<LocationReadOnlyList, string>(start));
        }

        private void DataPortal_Fetch(SingleCriteria<LocationReadOnlyList, string> criteria)
        {
            FetchAllStartingWith(criteria.Value);
        }

        private void FetchAllStartingWith(string start)
        {
            RaiseListChangedEvents = false;
            using (var ctx = ContextManager<Kiosk.Data.KioskDataContext>.GetManager(Database.ConnectionString))
            {
                var data = from o in ctx.DataContext.Location
                           where o.Name.ToLower().StartsWith(start.ToLower())
                           select o;
                IsReadOnly = false;
                foreach (var d in data)
                    this.Add(new LocationInfo(d.LocationID, d.Name, d.XCoordinate, d.YCoordinate));
                IsReadOnly = true;
            }
            RaiseListChangedEvents = true;
        }

    }
}
